<template>
  <div style="margin-top: 20px">
    <el-form size="mini" :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="关键字">
        <el-input v-model="searchForm.keyword" placeholder="宠物名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="toQuery">查询</el-button>
        <el-button type="primary" @click="toAdd">新增</el-button>
        <el-button type="warning" @click="batchRemove" :disabled="this.sels.length===0">已上线</el-button>
        <el-button type="default" @click="batchDownShelve" :disabled="this.sels.length===0">已下线</el-button>
<!--        <el-button type="warning" @click="toEdit(scope.row.id)">已上线</el-button>-->
<!--        <el-button type="default" @click="downShelve(scope.row.id)">已下线</el-button>-->
      </el-form-item>
    </el-form>
    <el-table
            :data="tableData"
            border
            style="width: 100%;"
            @selection-change="handleSelectionChange">
      <el-table-column
              type="selection"
              width="55">
      </el-table-column>
      <el-table-column
              prop="id"
              label="ID">
      </el-table-column>
      <el-table-column
              prop="name"
              label="名字">
      </el-table-column>
      <el-table-column
              prop="state"
              label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state == 0" type="info">已下线</el-tag>
          <el-tag v-if="scope.row.state == 1" type="warning">已上线</el-tag>
          <el-tag v-if="scope.row.state == 2" type="warning">已售</el-tag>
        </template>
      </el-table-column>
      <el-table-column
              prop="salecount"
              label="销量">
      </el-table-column>
      <el-table-column
              prop="saleprice"
              label="销售价格">
      </el-table-column>
      <el-table-column
              prop="costprice"
              label="成本价">
      </el-table-column>
      <el-table-column
              prop="onsaletime"
              label="上架时间">
      </el-table-column>

      <el-table-column label="操作" width="80px">
        <template slot-scope="scope">
          <!--          <el-button-->
          <!--              size="mini"-->
          <!--              @click="toEdit(scope.row.id)">编辑-->
          <!--          </el-button>-->
          <el-button type="warning"  size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="default" size="mini" @click="downEdit(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
<!--    &lt;!&ndash;分页条&ndash;&gt;-->
<!--    <el-col :span="24" class="toolbar">-->
<!--      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>-->
<!--      &lt;!&ndash;			current-change:自带的事件,我当前页发生改变时,就会触发此事件&ndash;&gt;-->
<!--      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="size" :total="total" style="float:right;">-->
<!--      </el-pagination>-->
<!--    </el-col>-->

    <el-pagination
            style="margin-top: 20px"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="searchForm.currentPage"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="searchForm.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
    </el-pagination>


    <el-dialog title="编辑宠物信息" :visible.sync="dialogPetFormVisible">
      <el-form ref="form" :model="petForm" label-width="80px">
        <el-form-item label="宠物名称">
          <el-input v-model="petForm.name"></el-input>
        </el-form-item>
        <el-form-item label="宠物图片">
          <el-upload
                  class="avatar-uploader"
                  action="http://localhost:80/file/ossUpload"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess"
                  :before-upload="beforeAvatarUpload">
            <img v-if="petForm.resources" :src="petForm.resources" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="销售价">
          <el-input v-model="petForm.saleprice"></el-input>
        </el-form-item>
        <el-form-item label="成本价">
          <el-input v-model="petForm.costprice"></el-input>
        </el-form-item>
        <el-form-item label="服务备注">
          <el-input type="textarea" v-model="petForm.intro"></el-input>
        </el-form-item>
        <el-form-item label="服务详情">
          <quill-editor
                  :options="editorOption"
                  v-model="petForm.adoptNotice"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="savePet()">保存</el-button>
        <el-button @click="dialogPetFormVisible=false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {quillEditor} from "vue-quill-editor"; //调用编辑器
  import 'quill/dist/quill.core.css';
  import 'quill/dist/quill.snow.css';
  import 'quill/dist/quill.bubble.css';

  import quillConfig from '../../common/js/quill-config'

  export default {
    name: "PetShengHe",
    components: { // 引入一个 自定义的components组件
      'quill-editor': quillEditor //QuillEditor  ---默认名称：quill-editor
    },
    data() {
      return {
        editorOption: quillConfig,
        betweenTime: null, //查询条件
        form: { //下面的3个属性，可省略
          id: '',//审核的数据的主键 ID
          title: '', //显示的标题
          note: '', //备注,
          state: null //审核状态
        },
        sels: [],//列表选中列
        petForm: {
          resources:'',

        },//宠物编辑表单
        dialogPetFormVisible: false, //宠物编辑弹框
        dialogFormVisible: false, //宠物审核弹框
        total: 0, //总记录数
        multipleSelection: [],
        searchForm: {
          page: 1,
          size: 10,
        },
        tableData: []
      }
    },
    methods: {
      savePet() {
        this.$http.post('/product', this.petForm).then(res => {
          let {success, message} = res.data;
          if (success) {
            this.dialogPetFormVisible = false;
            this.toQuery();
          }
          this.$message.success(message);
        })
      },
      toAdd() {
        this.petForm={};
        this.dialogPetFormVisible = true;
      },
      toEdit(id) {
        //根据主键ID查询宠物信息（带petDetail详情）
        this.$http.get("/product/shelve/" + id).then(res => {
          // this.petForm = res.data;
          // this.dialogPetFormVisible = true;
          let {message, success} = res.data;
          if (success) {
            //this.dialogFormVisible = false;
            this.$message.success(message);
            this.toQuery();
          } else {
            this.$message.error(message);
          }
        })

      },
      downShelve(id){
        this.$http.get("/product/downShelve/" + id).then(res => {
          // this.petForm = res.data;
          // this.dialogPetFormVisible = true;
          let {message, success} = res.data;
          if (success) {
            //this.dialogFormVisible = false;
            this.$message.success(message);
            this.toQuery();
          } else {
            this.$message.error(message);
          }
        })
      },
      downEdit(id){
        this.$http.get("/product/delete/" + id).then(res => {
          // this.petForm = res.data;
          // this.dialogPetFormVisible = true;
          let {message, success} = res.data;
          if (success) {
            //this.dialogFormVisible = false;
            this.$message.success(message);
            this.toQuery();
          } else {
            this.$message.error(message);
          }
        })
      },
      handleAvatarSuccess(res, file) {
        console.log(res)
        this.petForm.resources=res.data;
        this.imageUrl = URL.createObjectURL(file.raw);
      },
      beforeAvatarUpload(file) {
        console.log(file);
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
      shenghe(state) {
        this.form.state = state;
        this.$http.post('/searchmaster/shenghe/', this.form).then(res => {
          let {msg, success} = res.data;
          if (success) {
            this.dialogFormVisible = false;
            this.toQuery();
          } else {
            this.$message.error(msg);
          }
        })
      },
      toQuery() {
        if (this.betweenTime && this.betweenTime.length > 0) {//判断参数非空（!= '' && != underfind && != NAN）
          this.searchForm.betweenTime = this.betweenTime.join(",");
        } else {
          this.searchForm.betweenTime = ''
        }
        this.$http.post("/product/pageList", this.searchForm).then(res => {
          this.tableData = res.data.data.list;
          this.total = res.data.data.total;
        })
      },
      batchRemove: function () {
        var ids = this.sels.map(item => item.id)//去掉tostring
        this.$confirm('确认上线选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          //NProgress.start();
          let para = { ids: ids };
          console.log(ids)
          this.$http.post("/product/batchShelve",para).then((res) => {
            this.listLoading = false;
            //NProgress.done();
            this.$message({
              message: '上线成功',
              type: 'success'
            });
            this.toQuery();
          });
        }).catch(() => {

        });
      },
      batchDownShelve: function () {
        var ids = this.sels.map(item => item.id)//去掉tostring
        this.$confirm('确认下线选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          //NProgress.start();
          let para = { ids: ids };
          console.log(ids)
          this.$http.post("/product/downShelve",para).then((res) => {
            this.listLoading = false;
            //NProgress.done();
            this.$message({
              message: '下线成功',
              type: 'success'
            });
            this.toQuery();
          });
        }).catch(() => {

        });
      },
      //显示编辑界面
      handleEdit: function (index, row) {
        //显示编辑页面的模态框
        this.dialogPetFormVisible = true;
        this.petForm = row;
        console.log("当前修改的数据是:"+JSON.stringify(this.petForm))
      },
      handleSizeChange(val) {
        this.searchForm.size = val;
        this.toQuery();
      },
      handleCurrentChange(val) {
        this.searchForm.page = val;
        this.toQuery();
      },
      handleSelectionChange(sels) {
        this.sels = sels;
      }
    },
    mounted() {
      this.toQuery();
    }
  }
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
